<template>
  <div class="bigbox">
    <!-- 表头盒子 -->
    <div class="leftbox">
      <!-- 树节点过滤 -->
      <div>
        <el-input
          placeholder="输入关键字进行过滤"
          size="small"
          v-model="filterText"
        >
        </el-input>
      </div>
      <!-- 树组件 -->
      <div style="margin-top: 15px">
        <el-tree
          ref="tree"
          :props="props"
          :highlight-current="true"
          @node-click="handleNodeClick"
          :filter-node-method="filterNode"
          :load="loadNode"
          lazy
        >
        </el-tree>
      </div>
    </div>
    <!-- 盒子主体 -->
    <div class="bodybox">
      <!-- 新增按钮 -->
      <div class="top">
        <div class="title">短信应用配置</div>
        <button class="buttom" @click="submit('add')">
          <i class="el-icon-plus" style="font-size: 12px; color: #fff"></i>新增
        </button>
      </div>
      <div class="tabelbox">
        <el-table
          :header-cell-style="{
            'text-align': 'center',
            'background-color': '#f5f5f5',
          }"
          :data="tableData"
          border
          :cell-style="{'text-align':'center'}"
          style="width: 100%"
        >
        <template slot="empty">
              <el-empty description="暂无数据"></el-empty>
            </template>
          <el-table-column type="index" label="序号" width="100">
          </el-table-column>
          <el-table-column prop="orgName" label="公司名称"> </el-table-column>
          <el-table-column prop="smsType" label="短信类型">
            <template slot-scope="scope">
              <span>{{ scope.row.smsType}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="smsTemplateId" label="短信模板"> </el-table-column>
          <el-table-column prop="createTime" label="维护时间"> </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button type="text" style="font-size:18px;color: #4095e5" @click="edit(scope.row)"
              ><i class="el-icon-edit"></i
            ></el-button>
            <el-button type="text" style="font-size:18px;color: red" @click="del(scope.row.id)"
              ><i class="el-icon-delete"></i
            ></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <!-- 新增/修改弹框 -->
      <div>
        <el-dialog
          :show-close="false"
          :visible.sync="dialogVisible"
          width="500px"
          :close-on-click-modal="false"
          :destroy-on-close="true"
          :center="true"
        >
          <div class="dialog">
            <div class="title">短信应用配置信息</div>
            <!-- 企业下拉 -->
            <el-form
              ref="dialogForm"
              :rules="rules"
              :model="dialogForm"
              label-width="110px"
            >
              <el-form-item label="维护组织" prop="orgName">
                <el-select
                  disabled
                  v-model="dialogForm.orgName"
                  placeholder="请选择"
                >
                  <el-option
                    v-for="(item, index) in options"
                    :key="index"
                    :label="item.orgName"
                    :value="item.orgNo"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="短信类型:" prop="smsType">
                <el-select
                  v-model="dialogForm.smsType"
                  placeholder="请选择短信类型"
                  @change="change"
                >
                  <el-option
                    v-for="item in smsselect"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="短信模板:" prop="smsTemplate">
                <el-select
                  v-model="dialogForm.smsTemplate"
                  placeholder="请选择短信模板"
                  @change="change1"
                >
                  <el-option
                    v-for="item in SMStemplateList"
                    :key="item.id"
                    :label="item.name"
                    :value="item.id"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
            </el-form>
            <!-- 弹窗内容 -->
          </div>
          <div style="margin-top: 10px; text-align: center">
            <button
              @click="quxiao"
              style="
                width: 60px;
                height: 30px;
                margin-right: 10px;
                background-color: #fff;
                border: 1px solid #ccc;
                margin-top: 20px;
              "
            >
              取 消
            </button>
            <button
              @click="sure"
              style="
                width: 60px;
                height: 30px;
                background-color: #3b8eea;
                color: #fff;
                border: none;
                margin-top: 20px;
              "
            >
              确 定
            </button>
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import { gettreelist, gettreexiaji } from '../../api/userscreen'
import { delSMSConfigApi, getSMSConfigApi, getSMSSelectListApi, getSMStemplateApi, saveSMSConfigApi, updateSMSConfigApi } from '@/api/SMSuse'
export default {
  name: 'SMSuseIndex',
  data () {
    return {
      value: '',
      orgName: '',
      tableData: [],
      options: [],
      filterText: '',
      dialogForm: {},
      smsselect: [],
      SMStemplateList: [],
      rules: {
        smsType: [
          { required: true, message: '请选择短信类型', trigger: 'change' }
        ],
        smsTemplate: [
          { required: true, message: '请选择短信模板', trigger: 'change' }
        ]
      },
      page: {
        pageNo: 0,
        pageSize: 10
      },
      props: {
        label: 'bizOrgName',
        disabled: 'disabled'
      },
      dialogVisible: false,
      isedit: 'add'
    }
  },
  created () {
    this.getSMSSelectList()
  },
  async mounted () {},

  methods: {
    // 新增/修改
    submit (val) {
      this.isedit = val
      if (!this.dialogForm.orgName) {
        return this.$message.warning('请先选择企业')
      }
      this.dialogVisible = true
    },
    //  取消/弹框关闭
    quxiao () {
      this.dialogVisible = false
      this.dialogForm = {
        orgName: this.orgName,
        orgNo: this.value,
        smsType: '',
        smsTemplate: ''
      }
    },
    // 确认修改并保存
    async sure () {
      await this.$refs.dialogForm.validate(async valid => {
        if (valid) {
          const res = this.dialogForm.id ? await updateSMSConfigApi(this.dialogForm) : await saveSMSConfigApi(this.dialogForm)
          if (res?.code === 200) {
            this.$message.success('保存成功')
            this.dialogVisible = false
            this.quxiao()
            await this.getSMSConfig()
            this.$refs.dialogForm.resetFields()
          } else {
            this.$message.success('保存失败')
          }
          console.log(res)
        }
      })
    },
    // 删除头像
    // 树节点点击
    async handleNodeClick (val) {
      console.log(val)
      this.dialogForm.orgName = val.bizOrgName
      this.dialogForm.orgNo = val.bizOrgNo
      this.value = val.bizOrgNo
      this.orgName = val.bizOrgName
      this.getSMSConfig()
    },
    // 树组件懒加载
    async loadNode (node, resolve) {
      const res = await gettreelist()
      if (node.level === 0) {
        // 初始的级数（最顶层）
        return resolve(res?.data.list) // 最顶层数据渲染为region
      }
      const ress = await gettreexiaji({ orgNo: node.data.bizOrgNo })
      if (node.level === 1) {
        return resolve(ress?.data.list)
      } else if (node.level === 2) {
        return resolve([])
      }
    },
    // 树节点过滤
    filterNode (value, data) {
      if (!value) return true
      return data.bizOrgName.indexOf(value) !== -1
    },
    // 获取短信应用配置数据
    async getSMSConfig () {
      const res = await getSMSConfigApi({
        orgNo: this.value,
        ...this.page
      })
      this.tableData = res.data.list
      console.log(res.data.list)
    },
    // 获取短信类型下拉框数据
    async getSMSSelectList () {
      const res = await getSMSSelectListApi()
      console.log(res)
      this.smsselect = res?.data.list
      console.log('smsselect', this.smsselect)
    },
    change () {
      this.getSMStemplate()
      // this.dialogForm.smsTemplate = []
      this.$set(this.dialogForm, 'smsTemplate', '')
    },
    change1 () {
      this.$forceUpdate()
    },
    // 获取短信模板
    async getSMStemplate () {
      const res = await getSMStemplateApi({
        orgNo: this.dialogForm.orgNo,
        smsType: this.dialogForm.smsType
      })
      this.SMStemplateList = res.data?.list || []
    },
    // 编辑
    edit (item) {
      this.isedit = 'edit'
      console.log(item)
      this.dialogForm.orgNo = item.orgNo
      this.dialogForm.orgName = item.orgName
      this.dialogForm.smsType = item.smsType
      this.dialogForm.smsTemplate = item.smsTemplateId
      this.dialogForm.id = item.id
      this.getSMStemplate()
      this.dialogVisible = true
    },
    // 删除
    async del (id) {
      this.$confirm('此操作将永久删除该档案, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          try {
            const res = await delSMSConfigApi(id)
            if (res.code == 200) {
              this.$message.success('删除成功')
              this.getSMSConfig()
              this.$refs.dialogForm.resetFields()
            } else {
              this.$message.success('删除失败')
            }
          } catch (e) {
            console.log(e)
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      this.getSMSConfig()
    }
  },
  watch: {
    filterText (val) {
      this.$refs.tree.filter(val)
    }
  }
}
</script>

<style lang="scss" scoped>
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
.bigbox {
  background-color: #f1f1f1;
  min-height: 100%;
  padding: 10px;
  display: flex;
  .leftbox {
    width: 300px;
    background-color: #fff;
    padding: 20px;
  }
  .bodybox {
    flex: 1;
    margin-left: 10px;
    background-color: #fff;
    padding: 20px;
    .top {
      display: flex;
      justify-content: space-between;
      text-align: right;
      padding-bottom: 10px;
      margin-bottom: 20px;
      border-bottom: 1px solid #bbbbbb;
      .buttom {
        width: 70px;
        height: 30px;
        background-color: #1890ff;
        color: #fff;
        border-radius: 3px;
        border: none;
      }
    }
    .formbox {
      margin-top: 10px;
      display: flex;
      flex-wrap: wrap;
      .littlebox {
        position: relative;
        margin-right: 5%;
        margin-bottom: 20px;
        width: 28%;
        border: 1px solid #ccc;
        border-radius: 3px;
        padding: 10px;
        .boxbtn {
          position: absolute;
          top: 10px;
          right: 20px;
        }
        .contain {
          margin-top: 40px;
          padding-left: 21px;
          .el-row {
            margin-bottom: 30px;
          }
        }
      }
    }
    .dialog {
      .title {
        font-size: 28px;
        margin-bottom: 30px;
      }
      .text {
        font-size: 12px;
        color: #3b8eea;
      }
    }
  }
}

::v-deep .el-select {
  width: 100%;
}
::v-deep .el-input {
  height: 32px;
}
::v-deep .el-input__inner {
  height: 32px;
}
::v-deep .el-input__icon {
  line-height: 32px;
}
.el-select-dropdown__item {
  padding-left: 10px;
}
::v-deep .el-date-editor {
  width: 100%;
}

.el-form-item {
  margin-bottom: 20px;
}
button{
  cursor: pointer;
}
</style>
